package org.feng.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.feng.entity.pojo.Menu;
import org.feng.entity.vo.MenuTreeVO;
import org.feng.entity.vo.MenuVO;
import org.feng.pojo.Result;
import org.feng.service.MenuService;
import org.feng.utils.PoiUtils;
import org.feng.utils.TreeUtils;
import org.feng.utils.condition.Condition;
import org.feng.wrapper.MenuWrapper;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;


/**
 * 菜单表
 *
 * @author feng
 * @since 2020-12-09
 */
@RestController
@RequestMapping("/system/menu")
@AllArgsConstructor
@Api(value = "菜单", tags = "菜单")
public class MenuController {
    private final MenuService menuService;

    /**
     * 导出
     */
    @ApiOperation(value = "导出", notes = "导出")
    @PostMapping("/export")
    public void export() throws IOException {
        LinkedHashMap<String, String> map = new LinkedHashMap<>();
        map.put("name", "名称");
        map.put("code", "权限标记");
        map.put("path", "链接地址");
        map.put("sort", "排序");
        PoiUtils.exportExcelStream(map, menuService.list());
    }

    /**
     * 创建按钮组
     */
    @ApiOperation(value = "创建按钮", notes = "传输List菜单")
    @PostMapping("/createBtn")
    public Result<Boolean> createBtn(@RequestBody List<Menu> list) {
        return Result.status(menuService.createBtn(list));
    }

    /**
     * 菜单树
     */
    @ApiOperation(value = "权限菜单", notes = "权限菜单")
    @GetMapping("/treeList")
    public Result<List<MenuTreeVO>> treeList() {
        List<Menu> menuList = menuService.treeList();
        return Result.success(TreeUtils.treeOut(
                MenuWrapper.build().entityTreeListVO(menuList),
                MenuTreeVO::getId,
                MenuTreeVO::getParentId,
                MenuTreeVO::getChildren));
    }

    /**
     * 菜单树
     */
    @ApiOperation(value = "所有菜单", notes = "所有菜单")
    @GetMapping("/menuTreeList")
    public Result<List<MenuTreeVO>> menuTreeList(Menu menu) {
        List<Menu> menuList = menuService.list(Wrappers.<Menu>lambdaQuery()
                .orderByAsc(Menu::getSort)
                .like(StrUtil.isNotEmpty(menu.getName()), Menu::getName, menu.getName()));
        return Result.success(TreeUtils.treeOut(
                MenuWrapper.build().entityTreeListVO(menuList),
                MenuTreeVO::getId,
                MenuTreeVO::getParentId,
                MenuTreeVO::getChildren));
    }


    /**
     * 列表
     */
    @ApiOperation(value = "列表", notes = "全部数据")
    @GetMapping
    public Result<List<MenuVO>> list() {
        return Result.success(MenuWrapper.build().entityListVO(menuService.list()));
    }

    /**
     * 分页列表
     */
    @ApiOperation(value = "分页列表", notes = "分页数据")
    @GetMapping(value = "/page")
    public Result<IPage<MenuVO>> page() {
        return Result.success(MenuWrapper.build().entityPageVO(menuService.page(Condition.getPage())));
    }

    /**
     * 详情
     */
    @GetMapping(value = "/query")
    @ApiOperation(value = "数据详情", notes = "传输id")
    public Result<MenuVO> detail(@RequestParam Long id) {
        return Result.success(MenuWrapper.build().entityVO(menuService.getById(id)));
    }

    /**
     * 新增
     */
    @ApiOperation(value = "保存数据", notes = "传输对象")
    @PostMapping
    public Result<Boolean> save(@RequestBody Menu menu) {
        return Result.status(menuService.save(menu));
    }

    /**
     * 修改
     */
    @ApiOperation(value = "更新数据", notes = "传输对象")
    @PutMapping
    public Result<Boolean> update(@RequestBody Menu menu) {
        return Result.status(menuService.updateById(menu));
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除数据", notes = "传输id数组")
    @DeleteMapping
    public Result<Boolean> delete(@RequestParam String ids) {
        return Result.status(menuService.removeByIds(Arrays.asList(ids.split(","))));
    }

    public static void main(String[] args) {

    }
}
